﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace PJOnimal
{
    public class SelectCharacter : GameScene
    {
        Texture2D chonNhanVatText;
        Texture2D tabShow;
        Texture2D arrowShow;
        Texture2D[] characterCard = new Texture2D[6];
        Texture2D[] tieuSu = new Texture2D[6];
        Vector2[] toaDo = new Vector2[6];
        Vector2 toaDoTab;
        Rectangle[] hinhChuNhat = new Rectangle[6];
        bool[] vaCham = new bool[6];
        bool[] tieuSuShow = new bool[6];
        float[] colorFade = new float[6];
        Random random = new Random();

        public SelectCharacter(Onimal game)
            : base(game)
        {
            hinhNen = game.Content.Load<Texture2D>("HinhNen/Ayumi");
            chonNhanVatText = game.Content.Load<Texture2D>("UI/ChonNhanVatText");
            tabShow = game.Content.Load<Texture2D>("UI/TabShow");
            arrowShow = game.Content.Load<Texture2D>("UI/ArrowShow");

            Vector2 toaDoGoc = new Vector2(73, 100); //tọa độ gốc
            toaDoTab = new Vector2(397, 440);
            //game.dieuHanhGame.man2 = new Man2(game);
            for (int i = 0; i < 6; i++)
            {
                characterCard[i] = game.Content.Load<Texture2D>("Character Card/CharacterCard" + i);
                tieuSu[i] = game.Content.Load<Texture2D>("Character/TieuSu/TieuSu" + i);

                toaDo[0] = toaDoGoc;
                if (i != 0)
                    toaDo[i] = toaDo[i - 1] + new Vector2(110, 0);

                hinhChuNhat[i] = new Rectangle((int)toaDo[i].X, (int)toaDo[i].Y, characterCard[i].Width, characterCard[i].Height);
            }
        }

        public override void Update(GameTime gameTime)
        {
            for (int i = 0; i < 6; i++)
            {
                if (input.conChuot.Intersects(hinhChuNhat[i]))
                {
                    vaCham[i] = true;
                    tieuSuShow[i] = true;

                    if (colorFade[i] < 255)
                        colorFade[i] += 3f;

                    if (input.NhanChuot())
                    {
                        if (i != 5)
                            game.NhanVatSelect = i;
                        else
                            game.NhanVatSelect = random.Next(0, 5);

                        Character nhanvat = new Character(game);
                        game.dieuHanhGame.man2.nhanVat = nhanvat;
                        game.dieuHanhGame.ChuyenMan(game.dieuHanhGame.select, game.dieuHanhGame.man2);
                    }
                }
                else
                {
                    vaCham[i] = false;
                    tieuSuShow[i] = false;
                    colorFade[i] = 0f;
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(hinhNen,new Vector2(-500,-50), Color.White);
            spriteBatch.Draw(chonNhanVatText, new Vector2(180, 5), Color.White);

            spriteBatch.Draw(tabShow, toaDoTab, null, Color.White, 0f, new Vector2(tabShow.Width / 2, tabShow.Height / 2), 1f, SpriteEffects.None, 1f);
            
            if (input.chuotMoi.X > 120 && input.chuotMoi.X < 690)
                spriteBatch.Draw(arrowShow, new Vector2(input.chuotMoi.X - 35, 280 - 0.9f), Color.White);

            for (int i = 0; i < 6; i++)
            {
                spriteBatch.Draw(characterCard[i], toaDo[i], (vaCham[i]) ? Color.White : Color.DimGray);
                if (tieuSuShow[i])
                    spriteBatch.Draw(tieuSu[i], toaDoTab, null, new Color(255,255,255,(byte)colorFade[i]) , 0f, new Vector2(tieuSu[i].Width / 2, tieuSu[i].Height / 2), 1f, SpriteEffects.None, 1f);
            }

            base.Draw(spriteBatch);
        }
    }
}
